package com.iretailer.report.easyexcel.serivce;

import com.iretailer.report.easyexcel.data.CameraData;
import com.iretailer.report.model.*;
import com.iretailer.report.service.CameraService;
import com.iretailer.report.service.CameraZoneService;
import com.iretailer.report.service.CategoriesService;
import com.iretailer.report.service.MallsService;
import com.iretailer.report.util.SecurityUserUtil;
import com.iretailer.report.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: xjw
 * @Date: 2020/3/20 17:31
 * @Description:
 */
//@Component
public class ImportCamera {
    private final static Logger log = LoggerFactory.getLogger(ImportCamera.class);

    @Autowired
    private MallsService mallsService;
    @Autowired
    private CameraService cameraService;
    @Autowired
    private CameraZoneService cameraZoneService;
    @Autowired
    private CategoriesService categoriesService;

    public void excelAnalysis(List<CameraData> data) throws Exception {
        if (data != null && data.size() > 0) {
             User user = SecurityUserUtil.currentUser(null);
            List<Malls> allMalls = mallsService.list();
            Map<String, Malls> mallsMap = allMalls.stream().collect(Collectors.toMap(Malls::getName, a -> a));
            Map<String, List<CameraData>> dataMap = data.stream().collect(Collectors.groupingBy(CameraData::getDeviceId));
            List<Categories> cameraDeviceType = categoriesService.findChildrenCategoriesByKeyName("CameraDeviceType");
            Map<String, Categories> typeMap = cameraDeviceType.stream().collect(Collectors.toMap(Categories::getKeyName, a -> a));
            for (Map.Entry<String, List<CameraData>> entry : dataMap.entrySet()) {
                String deviceId = entry.getKey();
                List<CameraData> d = entry.getValue();
                Malls m = mallsMap.get(d.get(0).getMallName());
                if (m != null) {
                    if (!StringUtil.notEmpty(deviceId)) {
                        continue;
                    }
                    Camera camera = cameraService.selectByDeviceId(deviceId);
                    if (camera == null) {
                        Camera newCamera = new Camera();
                        LocalDateTime now = LocalDateTime.now();
                        newCamera.setCreateTime(now);
                        newCamera.setUpdateTime(now);
                        newCamera.setUpdateUser(user.getName());
                        newCamera.setZoneCount(d.size());
                        newCamera.setMallId(m.getId());
                        newCamera.setDeviceId(d.get(0).getDeviceId());
                        newCamera.setEnabled(1);
                        if (!typeMap.isEmpty()) {
                            String key = d.get(0).getDeviceType();
                            if (typeMap.containsKey(key)) {
                                newCamera.setDeviceType(typeMap.get(key).getKeyName());
                            } else {
                                newCamera.setDeviceType(cameraDeviceType.get(0).getKeyName());
                            }
                        }

                        // 插入Camera
                        cameraService.insert(newCamera);

                        //保存 更新 cameraZone
                        for (CameraData cd : d) {
                            CameraZone cz = new CameraZone();
                            cz.setEnabled(1);
                            cz.setCameraId(newCamera.getId());
//                            String zoneClass = cd.getZoneClass();
//                            if (StringUtil.notEmpty(zoneClass)) {
//                                cz.setZoneClass(zoneClass);
//                            } else {
//                                cz.setZoneClass("Count");
//
//                            }
                            String zone = cd.getZoneCode();
                            if (StringUtil.notEmpty(zone)) {
                                cz.setZone(zone);
                            } else {
                                cz.setZone("0");
                            }
                            cz.setUpdateTime(now);
                            cameraZoneService.saveOrUpdate(cz, user.getName());
                        }
                    }
                } else {
                    throw new Exception("MallName does not exist");
                }
            }
        }
    }
}
